package com.yun.datastructure.tree.binarytree.leetcode;

import com.yun.datastructure.tree.binarytree.domain.TreeNode;

//对称二叉树
public class Leetcode101 {
    public static void main(String[] args) {
        TreeNode root=new TreeNode(
                new TreeNode(new TreeNode(3),2,new TreeNode(4))
                ,1
                ,new TreeNode(new TreeNode(4),2,new TreeNode(3)));
        System.out.println(new Leetcode101().isSymmetric(root));
    }
    //方法1：递归
    public boolean isSymmetric(TreeNode root) {
        return check(root.left,root.right);
    }

    private boolean check(TreeNode left,TreeNode right){
        if(left==null&&right==null){
            return true;
        }
        if(left==null||right==null){
            return false;
        }
        if(left.val!= right.val){
            return false;
        }
        return check(left.left,right.right)&&check(left.right,right.left);
    }
}
